//
//  HSingleSeletView.m
//  NursingManages
//
//  Created by Zhen Huang on 17/9/26.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HSingleSeletView.h"

#import "MyExamTableViewCell.h"

#import "MyExamHeaderView.h"

#import "Header.h"
#import "Model_Hr_Question_Item.h"

#import "DIHTest.h"
#import "HShowALLQuesView.h"
#import "Model_Hr_ExamAnswer.h"
#import "InitDbTableExamOnLine.h"
#import "Model_Hr_ExamContent.h"
#import "HBaseViewController.h"
@interface HSingleSeletView ()<UITableViewDelegate, UITableViewDataSource,PassValueDelegate>

@property (strong, nonatomic) MyExamHeaderView *myExamHeaderView;

@property (strong, nonatomic) UITableView      *tableView;


@property (strong, nonatomic) NSIndexPath      *currentSelectIndex;

@property (nonatomic ,copy) NSString *title;

@property (nonatomic, strong)NSArray *dataArray;

@property (nonatomic, strong) Model_Hr_Question_Item *model;


@property (nonatomic, strong) UIView *footview;

@property (nonatomic, strong) UILabel * label;

@property (nonatomic, strong) UIButton *pageButon;

@property (nonatomic, assign) NSInteger pageIndex;

@property (nonatomic, strong) UIImageView *rightImage;

@property (nonatomic, strong) UIImageView *wrongImage;

@property (nonatomic, strong) UIButton  *rightButton;

@property (nonatomic, strong) UIButton *wrongButton;

@property (nonatomic, strong) NSMutableArray *rightArray;

@property (nonatomic, strong) NSMutableArray *wrongArray;

@property (nonatomic, strong) UIButton *sureButton;

@property (nonatomic, strong) UIButton *deleteButton;

@property (nonatomic, strong) NSMutableArray *isAnswerdArray;

@property (nonatomic, assign) BOOL isright;

@property (nonatomic, strong) Model_Hr_ExamAnswer *examModel;

@property (nonatomic, strong) Model_Hr_ExamContent *contentModel;

@property (nonatomic, copy) NSString *res_id;

@property (nonatomic, assign)NSInteger fromType;

@property (nonatomic, strong) NSMutableArray *examDetailArray;
@end

@implementation HSingleSeletView




- (instancetype)initWithconfigure:(NSInteger)index
                        dataArray:(NSArray *)dataArray
                       headHeight:(CGFloat)height
                        headTitle:(NSString *)title
                           typeid:(NSString *)type_id
                      onlineArray:(NSArray *)array
                           res_id:(NSString *)res_id
                         FromType:(NSInteger)fromType
                       examDetail:(NSArray *)examDetail{
    
    self = [super init];
    if (self) {
        Model_Hr_Question_Item *model = dataArray[index];
//        Model_Hr_ExamContent *contentModel = array[index];
        Model_Hr_ExamAnswer *answer = examDetail[index];
        self.examModel = answer;
//        self.contentModel = contentModel;
        self.model = model;
        self.title = title;
        self.dataArray = dataArray;
        self.pageIndex = index;
        [self initView];
        self.res_id = res_id;
        self.rightArray = [NSMutableArray new];
        self.wrongArray = [NSMutableArray new];
        self.isAnswerdArray = [NSMutableArray new];
        self.examDetailArray = examDetail;
        self.fromType = fromType;
        [self.tableView reloadData];
    }
    return self;
}

- (void)initView {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.tableView];
    
    self.myExamHeaderView.subjectTypeLabel.text = @"单选题";
    self.myExamHeaderView.subjectTypeLabel.textColor = [UIColor colorWithRed:0.40 green:0.83 blue:0.81 alpha:1.00];
    self.myExamHeaderView.subjectTypeLabel.layer.borderColor =[UIColor colorWithRed:0.40 green:0.83 blue:0.81 alpha:1.00].CGColor;
    self.myExamHeaderView.subjectLabel.text = self.title;
    _tableView.tableHeaderView = self.myExamHeaderView;
    CGRect newFrame = self.myExamHeaderView.frame;
    // 获取不确定文字的高度
    
    CGFloat testHeight = [self sizeWithFont:[UIFont systemFontOfSize:17.0] maxW:ScreenWidth-67 withContent:self.myExamHeaderView.subjectLabel.text] ;
    newFrame.size.height =   testHeight+60;
    self.myExamHeaderView.frame = newFrame;
    [self.tableView beginUpdates];
    
    [self.tableView setTableHeaderView: self.myExamHeaderView];
    self.tableView.tableFooterView = self.footview;
    
    [self.tableView endUpdates];
    
   
}




- (void)setSeleIndexStr:(NSString *)seleIndexStr {
    
    _seleIndexStr = seleIndexStr;
}

- (void)setSeleIndexPath:(NSIndexPath *)seleIndexPath {
    
    _seleIndexPath = seleIndexPath;
    _currentSelectIndex = _seleIndexPath;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.model.optionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *string   = @"UITableViewCell";
    
    MyExamTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    
    cell.selectedBtn.selected = NO;
    
    [self.rightArray removeAllObjects];
    [self.wrongArray removeAllObjects];
    [self.isAnswerdArray removeAllObjects];
    
    if (self.examModel.RES_ANSWER != nil) {
        
        [self.footview addSubview:self.sureButton];
        
//        [self.footview addSubview:self.deleteButton];
    }
    
    for (Model_Hr_ExamAnswer *model in self.examDetailArray) {
        
        if (model.RES_ANSWER != nil) {
        
            [self.isAnswerdArray addObject:model];
            
            if ([model.RES_ANSWER isEqualToString:self.model.ANSWER]) {
                
                [self.rightArray addObject:model];
            }else {
                [self.wrongArray addObject:model];
            }
        }
    }
    

    
    if (self.examModel.RES_ANSWER != nil) {
        
//        cell.userInteractionEnabled = NO;
        
        if ([cell.selectedBtn.titleLabel.text isEqualToString:self.examModel.RES_ANSWER]) {
            
            
            cell.selectedBtn.selected = YES;
            
        }
    }else {
//        cell.userInteractionEnabled = YES;
        cell.selectedBtn.selected = NO;

        
    }
    
    
    if (!cell) {
        
        cell = [[MyExamTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    
    NSString *option = self.model.optionArray[indexPath.row];
    
    switch (indexPath.row) {
            
        case 0:
            [cell.selectedBtn setTitle:@"A" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 1;
            break;
        case 1:
            [cell.selectedBtn setTitle:@"B" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 2;
            break;
        case 2:
            [cell.selectedBtn setTitle:@"C" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 3;
            break;
        case 3:
            [cell.selectedBtn setTitle:@"D" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 4;
            break;
        case 4:
            [cell.selectedBtn setTitle:@"E" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 5;
            break;
        case 5:
            [cell.selectedBtn setTitle:@"F" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 5;
            break;
        case 6:
            [cell.selectedBtn setTitle:@"G" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 5;
            break;
        case 7:
            [cell.selectedBtn setTitle:@"H" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 5;
            break;
        case 8:
            [cell.selectedBtn setTitle:@"I" forState:UIControlStateNormal];
            cell.selectedBtn.tag = 5;
            break;

            
        default:
            break;
    }
    cell.selectedBtn.layer.cornerRadius = 10;
    cell.selectlable.text = option;
    
    if (cell.selectedBtn.tag == [_seleIndexStr integerValue]) cell.selectedBtn.selected = YES;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    InitDbTableExamOnLine *initDB = [[InitDbTableExamOnLine alloc]init];
    
    [initDB deleteTableWithHSaTableCode:self.model.QUES_ID];
    
    
    if (_currentSelectIndex != nil && _currentSelectIndex != indexPath) {
        
        MyExamTableViewCell  *cell = [tableView cellForRowAtIndexPath:_currentSelectIndex];
        cell.selectedBtn.selected  = NO;
    }
    
    MyExamTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.selectedBtn.selected = !cell.selectedBtn.selected;
    
//    self.examModel.RES_ANSWER = cell.selectedBtn.titleLabel.text;
    
//    DIHTest *test = [[DIHTest alloc]init];
    
//    [test modelItem:self.model];
    
//    Model_Hr_ExamAnswer *model = [[Model_Hr_ExamAnswer alloc]init];
    self.examModel.QUES_ID = self.model.QUES_ID;
    self.examModel.TYPEID = self.model.TYPEID;
    self.examModel.RES_ANSWER = cell.selectedBtn.titleLabel.text;
    
//    model.RES_ANSWER = self.model.answers;
    self.examModel.RES_ID = self.res_id;
    self.examModel.QUES_SCORE  = self.model.ACTUAL_SCORE;
    
    
    if ([cell.selectedBtn.titleLabel.text isEqualToString:self.examModel.ANSWER]) {
        
        self.examModel.RIGHT_FLAG = 1;
        
    }else {
        
        self.examModel.RIGHT_FLAG = 0;
        
    }
    
    
    InitDbTableExamOnLine *initOnLine = [[InitDbTableExamOnLine alloc]init];
    
    [initOnLine inspectDetailModel:self.examModel];
    
    
    //    BOOL isright;
    
    [self.isAnswerdArray addObject:self.model];
    
    if ([cell.selectedBtn.titleLabel.text isEqualToString:self.model.ANSWER]) {
        
        [self.rightArray addObject:self.model];
        
        self.isright = 1;
        
    }else {
        [self.wrongArray addObject:self.model];
        
        self.isright = 0;
    }
    
    
    

    _currentSelectIndex = indexPath;
    
    [self.tableView reloadData];
}
// 计算指定文字的高度
-(CGFloat)sizeWithFont:(UIFont *)font maxW:(CGFloat) maxW withContent:(NSString *)testStr{
    
    NSDictionary *textAttrs = @{NSFontAttributeName : font};
    CGSize size = CGSizeMake(maxW, MAXFLOAT);
    return [testStr boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate        = self;
        _tableView.dataSource      = self;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

- (MyExamHeaderView *)myExamHeaderView {
    if (!_myExamHeaderView) {
        _myExamHeaderView = [[MyExamHeaderView alloc]init];
    }
    return _myExamHeaderView;
}
- (UIView *)footview
{
    if (!_footview) {
        _footview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 120)];
        
        [_footview addSubview:self.pageButon];
        _footview.userInteractionEnabled = YES;
        
    }
    return _footview;
}

- (UILabel *)label
{
    if (!_label) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake(10, 48, ScreenWidth - 20, 40)];
    }
    return _label;
}

- (UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sureButton.frame = CGRectMake(7, 5, ScreenWidth - 14, 35);
        _sureButton.layer.masksToBounds = YES;
        _sureButton.layer.cornerRadius = 6;
        [_sureButton setTitle:@"继续下一题" forState:UIControlStateNormal];
        _sureButton.backgroundColor = kNavBlueColor;
        _sureButton.tag = 1;
        [_sureButton addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _sureButton;
}

- (UIButton *)deleteButton
{
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteButton.frame = CGRectMake(self.sureButton.right + 5, 5, (ScreenWidth - 20)/2, 35);
        _deleteButton.layer.masksToBounds = YES;
        _deleteButton.layer.cornerRadius = 3;
        [_deleteButton setTitle:@"更改答案" forState:UIControlStateNormal];
        _deleteButton.backgroundColor = kNavBlueColor;
        _deleteButton.tag = 2;
        [_deleteButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}


-(UIButton *)pageButon
{
    if (!_pageButon) {
        _pageButon = [UIButton buttonWithType:UIButtonTypeCustom];
        _pageButon.frame = CGRectMake(ScreenWidth - 100, 90, 80, 20);
        _pageButon.contentHorizontalAlignment = 2;
        [_pageButon setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_pageButon setTitle:[NSString stringWithFormat:@"%ld/%ld",self.pageIndex + 1,self.dataArray.count] forState:UIControlStateNormal];
        [_pageButon addTarget:self action:@selector(showAllQues:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pageButon;
}

- (UIImageView *)rightImage
{
    if (!_rightImage) {
        _rightImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 90, 20, 20)];
        _rightImage.image = [UIImage imageNamed:@"icon_correct3"];
    }
    return _rightImage;
}
- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(self.rightImage.right + 5, 90, 25, 25);
        _rightButton.userInteractionEnabled = YES;
        [_rightButton setTitleColor:RGB(69, 185, 124) forState:UIControlStateNormal];
        [_rightButton addTarget:self action:@selector(showAllQues:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _rightButton;
}

- (UIImageView *)wrongImage
{
    if (!_wrongImage) {
        _wrongImage = [[UIImageView alloc]initWithFrame:CGRectMake(70, 90, 20, 20)];
        _wrongImage.image = [UIImage imageNamed:@"icon_wrong"];
    }
    return _wrongImage;
}
- (UIButton *)wrongButton
{
    if (!_wrongButton) {
        _wrongButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _wrongButton.frame = CGRectMake(self.wrongImage.right + 5, 90, 25, 25);
        _wrongButton.userInteractionEnabled = YES;
        [_wrongButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_wrongButton addTarget:self action:@selector(showAllQues:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _wrongButton;
}

- (void)sure:(UIButton *)sender {
    
    self.examModel.QUES_ID = self.model.QUES_ID;
    
    if (self.SingleSelectBlock) {
        
        self.SingleSelectBlock(_currentSelectIndex, 1 ,self.isright,1);
    }
    
}

- (void)delete:(UIButton *)sender {
    
//    DIHTest *test = [[DIHTest alloc]init];
//    
//    [test updateDataWithQuesid:self.model.QUES_ID];
//    
//    self.model.answers = nil;
    
    InitDbTableExamOnLine *initDB = [[InitDbTableExamOnLine alloc]init];
    
    [initDB deleteTableWithHSaTableCode:self.contentModel.QUES_ID];
    
    self.examModel.RES_ANSWER = nil;
    
    [self.tableView reloadData];
    
}

- (void)showAllQues:(UIButton *)sender {
    
    self.infoView = [[HShowALLQuesView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 500, ScreenWidth, 500)];
    self.infoView.dataArray = self.dataArray;
    
    self.infoView.examArray = self.examDetailArray;
    
    self.infoView.delegate = self;
    
    [self addSubview:self.infoView];
    
    self.infoView.type = 2;
    
    self.infoView.isTestOrExam = 2;
    
    self.infoView.onlyTest = 0;
    UIView *view = [self.infoView viewWithTag:1000];
    [UIView animateWithDuration:0.5 animations:^{
        view.frame = CGRectMake(0, self.bounds.size.height-500,self.bounds.size.width, 500);
    }];
}

- (void)passValueByDelegateWithString:(NSInteger )string {
    
    if (self.SingleSelectBlock) {
        
        self.SingleSelectBlock(_currentSelectIndex, string ,self.isright,2);
    }
}

@end
